const app = getApp(),url = app.globalData.url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title:'',
    workDescribe:'',
    startTime:'',
    endTime:'',

    cat:true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log()
    that.setData({id:options.id});;
    if(options.id > 0){
      wx.request({
        url: url + 'cvSchoolPosition/getCVSchoolPositionById',
        method: "GET",
        data:{id:options.id },
        success:function(res){
          console.log("12345689",res);
          var ret = res.data
          if(ret.code == 200){
            that.setData({
              id:ret.data.id,
              title:ret.data.title,
              startTime:ret.data.startTime,
              endTime:ret.data.endTime,
              workDescribe:ret.data.workDescribe,
            })
          }else{
            that.setData({jldetail:''})
            wx.showToast({
              title: ret.msg,
              icon:'none'
            })
          }
        }
      })
    }else{
      console.log('=0')
      return false
    }
  },
  // 删除经历
  del(){
    var that = this
    var id = that.data.id
    wx.request({
      url: url + 'cvSchoolPosition/delete?id='+id,
      method: "DELETE",
      // data:{id:id },
      success:function(res){
        console.log("删除",res);
        var ret = res.data
        if(ret.code == 200){
          wx.showToast({
            title: '删除成功',
            icon:'none'
          })
          setTimeout(function(){
            wx.navigateBack({
              delta: 1,
            })
          },1000)
          
        }else{
          
          that.setData({sub:true})
          wx.showToast({
            title: '删除失败',
            icon:'none'
          })
        }
      }
    })
  },
  pjtitle(e){
    var that = this
    that.setData({title:e.detail.value})
  },
  workDescribe(e){
    var that = this
    that.setData({workDescribe:e.detail.value})
  },
  bindDateChange: function(e) {
    console.log('startTime', e.detail.value)
    this.setData({
      startTime: e.detail.value
    })
  },
  jieshuChange: function(e) {
    console.log('endTime', e.detail.value)
    this.setData({
      endTime: e.detail.value
    })
  },
  checkSensWords(e){
    let itemData = e.target.dataset;
    let label = Object.values(itemData)[0]
    let key = Object.keys(itemData)[0]
    this.sensWords = this.sensWords || {}
    app.validSensWords(e.detail.value, label).then(() => {
      delete this.sensWords[key]
    }).catch(() => {
      this.sensWords = {...itemData, ...this.sensWords}
    })
  },
  // 提交
  savedata(e){
    var that = this
    var cao = that.data.cao
    if(cao){
      that.setData({cao:false})
      console.log(cao)
      if(that.data.title == ''){
        wx.showToast({
          title: '请输入职务名称',
          icon:'none',
        })
        that.setData({cao:true})
        return false
      }
      if(that.data.workDescribe == ''){
        wx.showToast({
          title: '请输入项目描述',
          icon:'none',
        })
        that.setData({cao:true})
        return false
      }
      if(that.data.startTime == ''){
        wx.showToast({
          title: '请选择开始时间',
          icon:'none',
        })
        that.setData({cao:true})
        return false
      }
      if(that.data.endTime == ''){
        wx.showToast({
          title: '请选择结束时间',
          icon:'none',
        })
        that.setData({cao:true})
        return false
      }
      let sensWords = Object.values(this.sensWords || {})
      if(sensWords.length > 0){
        wx.showToast({
          title: `${sensWords.join(',')}包含敏感词，请修改后再保存`,
          icon:'none',
        })
        return false;
      }
      var jiliid = that.data.id
      var uid = wx.getStorageSync('user_massage')
      var data = {
        "title": that.data.title,//	项目名称
        "workDescribe": that.data.workDescribe,//项目描述
        "startTime": that.data.startTime,//项目开始时间
        "endTime": that.data.endTime,//项目结束时间
        "userUid": uid.userUid,//用户编号
      }
      if(jiliid > 0){
        //编辑
        var method = 'PUT'
        var api = 'cvSchoolPosition/updateSchoolPosition'
        data.id = jiliid
      }else{
        // 添加
        var method = 'POST'
        var api = 'cvSchoolPosition/add'
      }

      wx.request({
        url:url + api,
        header:{'context-type': 'application/json',},
        method: method,
        data:data,
        success:function(res){
          var ret = res.data
          if(ret.code == 200){
              that.setData({cao:false})
              wx.showToast({
                title: '保存成功',
                icon:'none',
              })
              setTimeout(function(){
                wx.navigateBack({
                  delta: 1,
                })
              },1000)
          }else{
            that.setData({cao:false})
            wx.showToast({
              title: '保存失败',
              icon:'none',
            })
          }
        }
      })
    }else{
      that.setData({
        cao:true
      })
      console.log('cao')
    }

    },

})